package com.asialjim.wechat.utils;

import org.apache.commons.lang3.StringUtils;
import java.util.*;

/**
 * <h1><em>ASIAL JIM JAVA DOC</em></h1><hr/>
 * <h2>CLASS DESCRIPTION <i>[ NAME: MapUtils ]</i></h2><strong>
 * <p> map utils
 * </strong><p><p><em>Copyright &copy; Asial Jim Co., LTD</em><hr/>
 *
 * @author Asial Jim &nbsp;&nbsp; <span>Email:<a href="mailto:asialjim@hotmail.com">asialjim@hotmail.com</a> &nbsp;&nbsp; <a href="asialjim@qq.com">asialjim@qq.com</a></span>
 * @version 1.0.0
 * @since 2021/3/18   &nbsp;&nbsp; JDK 8
 */
public class MapUtils {

    public static Map<String,Object> cleanNullValue(Map<String,Object> map){
        Map<String,Object> result = new HashMap<>();

        if (Objects.isNull(map))
            return result;

        Set<String> keys = map.keySet();
        for (String key : keys) {
            if (Objects.isNull(key)){
                continue;
            }

            Object value = map.get(key);
            if (Objects.isNull(value)){
                continue;
            }

            if (value instanceof String ){
                if (StringUtils.isNotBlank((CharSequence) value)) {
                    result.put(key, value);
                }
                continue;
            }

            if (value instanceof Map){
                Map<String,Object> sonMap = (Map<String,Object>) value;
                if (Objects.nonNull(sonMap) && sonMap.size() > 0) {
                    if (sonMap.entrySet().stream().map(Map.Entry::getValue).anyMatch(Objects::nonNull)) {
                        result.put(key, cleanNullValue((Map<String,Object>) value));
                    }
                }
            }

            if (value instanceof Collection){

                Collection collection = (Collection) value;
                if (Objects.isNull(collection) || collection.size() == 0){
                    continue;
                }

                if (collection.stream().anyMatch(Objects::nonNull)) {
                    result.put(key, cleanCollection(collection));
                }
            }

            if (value instanceof Object[]){
                Object[] array = (Object[]) value;
                if (Objects.isNull(array) || array.length == 0){
                    continue;
                }
                Collection sonCollection = Arrays.asList(array);
                Object[] objects = cleanCollection(sonCollection).toArray();
                result.put(key,objects);
            }
        }
        return result;
    }

    public static Collection cleanCollection(Collection collection){
        Collection result = new ArrayList();
        Iterator iterator = collection.iterator();
        while (iterator.hasNext()){
            Object next = iterator.next();

            if (Objects.isNull(next)){
                continue;
            }

            if (next instanceof Collection){
                Collection sonCollection = (Collection) next;
                if (Objects.isNull(sonCollection) || sonCollection.size() == 0){
                    continue;
                }
                result.add(cleanCollection(sonCollection));
            }

            if (next instanceof Map){
                Map<String,Object> map = (Map<String, Object>) next;
                if (map.entrySet().stream().map(Map.Entry::getValue).allMatch(Objects::isNull)){
                    continue;
                }

                result.add(map);
            }

            if (next instanceof Object[]){
                Object[] array = (Object[]) next;
                if (Objects.isNull(array) || array.length == 0){
                    continue;
                }
                Collection sonCollection = Arrays.asList(array);
                Object[] objects = cleanCollection(sonCollection).toArray();
                result.add(objects);
            } else{
                result.add(next);
            }
        }

        return result;
    }
}

